package router

import (
	"net/http"
	"tushare/common"
	"tushare/model"
	"tushare/service/aliyun/ecs"
	"tushare/service/appCommand"
	"tushare/service/task"

	"github.com/gin-gonic/gin"

	"encoding/json"
	"fmt"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
)

type Router struct {
	R *gin.Engine
}

func (rt *Router) InitRouter() {
	//是有session
	store := cookie.NewStore([]byte("secret"))
	rt.R.Use(sessions.Sessions("mysession", store))
	rt.R.Use(common.Cors())

	rt.tushare()
	rt.user()
	r := rt.R

	r.GET("/html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", gin.H{})
	})

	r.GET("/stock", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tushare_stock.html", gin.H{})
	})
	r.GET("/tushareMoneyFlowList", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tushare_moneyflow.html", gin.H{})
	})
	r.GET("/tushareCapitalInflow", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tushare_capitalInflow.html", gin.H{})
	})
	r.GET("/referenceSubstanceList", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tushare_reference_substance_list.html", gin.H{})
	})
	r.GET("/tushareRaisingLimit", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tushare_raising_limit.html", gin.H{})
	})
	r.GET("/login", func(c *gin.Context) {
		name := c.Query("name")
		if name == "" {
			common.ResponseError(c, 1001, "登录名不能为空")
			return
		}

		password := c.Query("password")
		if password == "" {
			common.ResponseError(c, 1001, "密码不能为空")
			return
		}

		userInfo := (new(model.Users)).UserInfo(name)
		fmt.Println(userInfo)
		if userInfo.Id == 0 {
			common.ResponseError(c, 1001, "用户不存在")
			return
		}

		if password != userInfo.Password {
			common.ResponseError(c, 1001, "密码错误")
			return
		}

		jsonStr, _ := json.Marshal(common.UserSession{Id: userInfo.Id, Name: userInfo.Name})

		session := sessions.Default(c)
		session.Set("user", string(jsonStr))
		session.Save()
		//{"code":20000,"data":{"token":"admin-token"}}
		var data = make(map[string]string)
		data["token"] = "admin-token"
		c.JSON(200, gin.H{"code": 20000, "message": "登录成功", "data": data})
	})

	r.GET("/LoginOut", func(c *gin.Context) {
		session := sessions.Default(c)
		session.Delete("user")
		session.Save()
		c.JSON(200, gin.H{"code": 20000, "message": "退出登录"})
	})

	r.GET("/isLogin", func(c *gin.Context) {
		session := sessions.Default(c)
		userInfo := session.Get("user")
		if userInfo == nil {
			c.JSON(200, gin.H{"code": 1001, "message": "请登录"})
			return
		}
		c.JSON(200, gin.H{"code": 0, "message": "已登录"})
		return
	})

	r.GET("/info", func(c *gin.Context) {
		//{roles: ['admin'],introduction: 'I am a super administrator',avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',name: 'Super Admin'}
		var data = make(map[string]interface{})
		roles := []string{"admin"}
		data["roles"] = roles
		data["introduction"] = "I am a super administrator"
		data["avatar"] = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
		data["name"] = "Super Admin"
		c.JSON(200, gin.H{"code": 20000, "data": data})
		return
	})

	aliyunEcsGroup := r.Group("/aliyunEcs/")
	{
		aliyunEcsGroup.Use(common.Auth())
		{
			aliyunEcsGroup.GET("ecsList", func(c *gin.Context) {
				list, count := ecs.EcsList(c)
				c.JSON(200, gin.H{"code": 0, "message": "成功", "data": list, "count": count})
			})

			aliyunEcsGroup.GET("ecs", func(c *gin.Context) {
				c.JSON(200, gin.H{"code": 0, "message": "成功"})
			})

			aliyunEcsGroup.GET("install_hadoop", func(c *gin.Context) {
				ecs.InstallHadoop(c)
			})

			aliyunEcsGroup.GET("createEcs", func(c *gin.Context) {
				ecs.CreateEcs(c)
			})

			aliyunEcsGroup.GET("deleteInstance", func(c *gin.Context) {
				ecs.DeleteInstance(c)
			})

			aliyunEcsGroup.GET("ecsAppList", func(c *gin.Context) {
				ecs.EcsAppList(c)
			})

			aliyunEcsGroup.GET("ecsAppInstallTask", func(c *gin.Context) {
				taskObj := new(task.Task)
				taskObj.CreateUserTask(c)
			})

			aliyunEcsGroup.GET("startApp", func(c *gin.Context) {
				am := new(appCommand.AppManage)
				am.StartApp(c)
			})

			aliyunEcsGroup.GET("stopApp", func(c *gin.Context) {
				am := new(appCommand.AppManage)
				am.StopApp(c)
			})

			aliyunEcsGroup.GET("reloadApp", func(c *gin.Context) {
				am := new(appCommand.AppManage)
				am.ReloadApp(c)
			})
		}
	}

}
